package yxy.game.pm2.module.union.war;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.redisson.api.RMap;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.game.pm2.bean.module.BaseGameRedisBean;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.module.battle.TeamModule;

/** 据点信息 */
public class UnionWarFort extends BaseGameRedisBean {

	private int index;// 据点序号

	private String playerCode;// 玩家code
	private String name;// 名字快照
	private int level;// 等级快照
	private int head;// 头像快照
	private int headframe;// 头像框快照
	private long power;// 战力
	private int star;// 已被攻占的星数
	private TeamModule team;// 队伍快照

	private ArrayList<UnionWarRecord> records = new ArrayList<>();

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public String getPlayerCode() {
		return playerCode;
	}

	public void setPlayerCode(String playerCode) {
		this.playerCode = playerCode;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public int getHead() {
		return head;
	}

	public void setHead(int head) {
		this.head = head;
	}

	public int getHeadframe() {
		return headframe;
	}

	public void setHeadframe(int headframe) {
		this.headframe = headframe;
	}

	public long getPower() {
		return power;
	}

	public void setPower(long power) {
		this.power = power;
	}

	public int getStar() {
		return star;
	}

	public void setStar(int star) {
		this.star = star;
	}

	public TeamModule getTeam() {
		return team;
	}

	public void setTeam(TeamModule team) {
		this.team = team;
	}

	public ArrayList<UnionWarRecord> getRecords() {
		return records;
	}

	public void setRecords(ArrayList<UnionWarRecord> records) {
		this.records = records;
	}

	// redis==========================================================================

	static public String key(String unionCode) {
		return String.format("UNION:%s:MODULE:WARFORT", unionCode);
	}

	static private RMap<String, UnionWarFort> touch(String unionCode) throws RedisException {
		return RedisManager.client(redis()).getMap(key(unionCode));
	}

	static public Map<String, UnionWarFort> all(String unionCode) {
		try {
			Map<String, UnionWarFort> forts = touch(unionCode).readAllMap();
			return forts;
		} catch (RedisException e) {
		}
		return new HashMap<>();
	}

	static public UnionWarFort get(String unionCode, String fortCode) {
		try {
			UnionWarFort fort = touch(unionCode).get(fortCode);
			return fort;
		} catch (Exception e) {
			return null;
		}
	}

	static public boolean set(String unionCode, UnionWarFort fort) {
		try {
			RMap<String, UnionWarFort> forts = touch(unionCode);
			forts.put(fort.getPlayerCode(), fort);
			return true;
		} catch (RedisException e) {
			e.printStackTrace();
			return false;
		}
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return null;
	}

	public static RedisLock lock(String unionCode) {
		return getLock(String.format("lock:%s", key(unionCode)));
	}

}
